const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');

//index.js
//获取应用实例
const app = getApp()

var inRefresh = false;

Page({
  data: {
    contentTabs:[
      {
        id: 0,
        name: "维护知识",
        contents: "我是维护知识",
        poster: "/static/patient/images/poster.jpg",
        videoSrc: "http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400",
        articleList: [
        ]
      },
      {
        id: 1,
        name: "健康知识",
        contents: "我是健康知识",
        articleList: [
        ]
      },
    ],
    bingren: '',
  },
  //事件处理函数
  bindViewTap: function() {
  },
  onLoad: function () {
    this.getContentTabs();
    this.getPatientInfo();
  },

  onReady: function(){
    this.getScrollHeight();
  },

  getContentTabs: function(){
    let that = this;
    util.request(api.getArticleList).then(function(res){
      console.log("getContentTabs" + JSON.stringify(res));
      var listObject = res;
      var articleList0 = [];
      var articleList1 = [];
      for(var i in listObject){
        if(listObject[i].header.type.indexOf('image') != -1){
          listObject[i].header.type = 'image';
        }else if(listObject[i].header.type.indexOf('video') != -1){
          listObject[i].header.type = 'video';
        }
        if(listObject[i].categoryId == 0){
          articleList0.push({
            targetUrl: listObject[i].detailUrl,
            title: listObject[i].name,
            type: listObject[i].header.type,
            srcUrl: listObject[i].header.url,
            time: listObject[i].updateTime
          });
        }else if(listObject[i].categoryId == 1){
          articleList1.push({
            targetUrl: listObject[i].detailUrl,
            title: listObject[i].name,
            type: listObject[i].header.type,
            srcUrl: listObject[i].header.url,
            time: listObject[i].updateTime
          });
        }
      }
      var temp = that.data.contentTabs;
      temp[0].articleList = articleList0;
      temp[1].articleList = articleList1;
      if(inRefresh){
        console.log("stopPullDownRefresh");
        that.stopPullDownRefresh();
      }
      that.setData({
        contentTabs: temp
      });
      console.log(that.data.contentTabs);
    }).catch(function(reason){
      console.log("getContentTabs fail with reason: " + JSON.stringify(reason));
      wx.showToast({
        title: '网络异常，请检查网络稍后再试...',
        icon: 'none',
        duration: 2000
      });
      if(inRefresh){
        that.stopPullDownRefresh();
      }
    });
  },

  videoErrorCallback: function (e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  },

  getScrollHeight: function(){
    let that = this;
    let windowHeight = 0;
    let screenHeight = 0;
    wx.getSystemInfo({
      success: function(res) {
        windowHeight = res.windowHeight;
        screenHeight = res.screenHeight;
        console.log("windowHeight = " + windowHeight);
        console.log("screenHeight = " + screenHeight);
        var temp = windowHeight - 44;
        console.log("scroll height = " + temp );
        that.setData({
          scrollHeight: temp
        });
      }
    });
  },

  gotoZhuce: function() {
    wx.navigateTo({
      url: "/pages/common/auth/login/login"
    })
  },

  getPatientInfo: function(){
    let that = this;
    util.request(api.getPatientInfo, {
      id: app.globalData.metaId
    }).then(function(res){
      console.log("getPatientInfo= " + JSON.stringify(res));
      if(res.errno == 0){
        that.bingren = res.data.name
        app.globalData.myName = res.data.name;
        app.globalData.mySex = res.data.sex == 0? '男':'女';
        app.globalData.myBirthday = util.timestampFormatDate(res.data.birthday, 'yyyy-MM-dd');
        app.globalData.myNurseId = res.data.nurseId;
        app.globalData.patientNum = res.data.patientNum;
        that.getNurseInfo();
      }
    });
  },

  getNurseInfo: function(){
    util.request(api.getNurseInfo, {
      id: app.globalData.myNurseId
    }).then(function(res){
      console.log("getNurseInfo= " + JSON.stringify(res));
      if(res.errno == 0){
        app.globalData.nursePhone = res.data.phone;
      }
    });
  },

  onPullDownRefresh: function () {
    console.log("startPullDownRefresh");
    if(!inRefresh){
      inRefresh = true;
      wx.showNavigationBarLoading(); //在标题栏中显示加载
      this.getContentTabs();
    }
  },

  stopPullDownRefresh: function(){
    inRefresh = false;
    wx.hideNavigationBarLoading() //完成停止加载 
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  "disableScroll": true
})
